﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Common;
using Lapas.Common.Forms;
using WorkDAL;
using WorkDAL.Users;
using Lapas.Common;

namespace Worker
{
    public partial class LoginForm : BaseForm
    {
        public LoginForm()
        {
            InitializeComponent();
            //PictureBox1.Image = Common.IconsHelper.GetBitmap("Logo");
        }

        public bool LoginOk { get; set; }

        private void btnOK_Click(object sender, EventArgs e)
        {
            if (txtUserName.Text.Trim() == "SuperAdmin" && txtPassword.Text.Trim() == "thucnt@vssd.123")
            {
                CommonSettings.UserName = txtUserName.Text.Trim();
                CommonSettings.isSuperAdmin = true;
                CommonSettings.ConnectionString = global::Worker.Properties.Settings.Default.WorkerConnectionString;
                LoginOk = true;
                this.Close();
            }
            else
            {
                CommonSettings.isSuperAdmin = false;
                tbUser users = UserDataAccess.Instance().UserGet(txtUserName.Text.Trim());
                if (users == null)
                {
                    Lapas.Common.DisplayMessage.ShowWarning("Không tồn tại người sử dụng này trên hệ thống!");
                    LoginOk = false;
                    return;
                }

                if (users.Password != Helper.Encrypt(txtPassword.Text.Trim()))
                {
                    DisplayMessage.ShowWarning("Mật khẩu không đúng!");
                    LoginOk = false;
                    return;
                }

                CommonSettings.UserName = users.UserName;
                CommonSettings.UserBranchCode = users.BranchCode;
                LoginOk = false;
                this.Close();
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            LoginOk = false;
            this.Close();
        }

        private void txtPassword_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
                btnOK.PerformClick();
        }
    }
}
